//
//  NewFeatureController.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/23.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit

private let reuseIdentifier = "NewFeatureControllerCell"

class NewFeatureController: UICollectionViewController {

    // MARK:- 属性
    private var layout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    // MARK:- 懒加载
    
    // MARK:- 初始化方法
    //系统指定的初始化方法是带参数的，所以不用写override
    init() {
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK:- 生命周期
    override func viewDidLoad() {
        super.viewDidLoad()
        //1.注册一个cell
        collectionView?.register(NewfeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        ///1.设置布局，最好在这个方法中设置
        //设置item大小
        layout.itemSize = UIScreen.main.bounds.size
        //设置间隙,水平和垂直
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .horizontal
        //2.设置collection的属性
        collectionView?.showsHorizontalScrollIndicator = false
        //回弹效果
        collectionView?.bounces = false
        //设置分页
        collectionView?.isPagingEnabled = true
    }
}

// MARK:- collectionView协议方法
extension NewFeatureController{
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 4
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        //1.获取cell
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! NewfeatureCell
        //2.设置cell的数据
        cell.imageIndex = indexPath.item + 1
        print(indexPath.item)
        return cell
    }
    
}


// MARK:- 自定义cell类
private class NewfeatureCell : UICollectionViewCell{
    var imageIndex : Int?{
        didSet{
            let named = "new_feature_" + "\(imageIndex!)"
            print(named)
            iconImageView.image = UIImage(named: named)
            if imageIndex == 4 {
                startButton.isHidden = false
                //执行动画
                //缩放
                startButton.transform =
                    CGAffineTransform.init(scaleX: 0.0, y: 0.0)
                startButton.isEnabled = false
                UIView.animate(withDuration: 3.2, delay: 0, usingSpringWithDamping: 1.2, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue:0), animations: {
                    //清空形变
                    self.startButton.transform = CGAffineTransform.identity
                }, completion: { (_) in
                    self.startButton.isEnabled = true
                })
            }else{
                startButton.isHidden = true
            }
        }
    }
    
    private lazy var iconImageView = UIImageView()
    private lazy var startButton : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named:"new_feature_button"), for: .normal)
        btn.setBackgroundImage(UIImage(named:"new_feature_button_highlighted"), for: .highlighted)
        btn.center = CGPoint(x: UIScreen.main.bounds.size.width / 2, y: UIScreen.main.bounds.size.height - 150)
        btn.bounds = CGRect(x: 0, y: 0, width: 186, height: 42)
        btn.isHidden = true
        btn.addTarget(self, action: #selector(startButtonClick(button:)), for: .touchUpInside)
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
   private func setupUI(){
        //1.添加子控件到contentView上
    
       iconImageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.height)
        contentView.addSubview(iconImageView)
       contentView.addSubview(startButton)
        //2.布局子控件的位置
    
    
    }
    
    func startButtonClick(button:UIButton) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "rootVc"), object: nil)
    }
}










